//
//  ACSearchResultController.m
//  ACFramework
//
//  Created by caoyq on 2017/12/13.
//

#import "ACSearchResultController.h"

@interface ACSearchResultController ()

@end

@implementation ACSearchResultController

- (NSMutableArray *)searchResultArray {
    if (_searchResultArray == nil) {
        _searchResultArray = [NSMutableArray array];
    }
    return _searchResultArray;
}

- (UISearchController *)searchController {
    if (_searchController == nil) {
        _searchController = [[UISearchController alloc] initWithSearchResultsController:self];
        _searchController.searchBar.showsCancelButton = NO;
        //    search.searchResultsUpdater = self;
        //搜索框上升，是否需要模糊背景,其中模糊背景存在时搜索出来的结果是没法选中的，会退出搜索，下面显示搜索出来的结果   default is YES
        _searchController.dimsBackgroundDuringPresentation = NO;
        //开始搜索是自动升起，然后隐藏navbar
//        _searchController.hidesNavigationBarDuringPresentation = YES;
        _searchController.searchBar.placeholder = @"搜索";
        [_searchController.searchBar sizeToFit];
        //皮肤颜色，包括cancel按钮，和搜索的文字
        //_searchController.searchBar.tintColor = [UIColor redColor];
        //searchBar的边缘，外圈的颜色
        _searchController.searchBar.barTintColor = [UIColor colorWithRed:0xc7/255.0 green:0xc9/255.0 blue:0xce/255.0 alpha:1.000];
        
    }
    return _searchController;
}

#pragma mark - Life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setEdgesForExtendedLayout:UIRectEdgeNone];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableFooterView = [[UIView alloc] init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_numberOfSectionsInTableViewCompletion) {
        return _numberOfSectionsInTableViewCompletion(tableView);
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_numberOfRowsInSectionCompletion) {
        return _numberOfRowsInSectionCompletion(tableView, section);
    }
    return self.searchResultArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_cellForRowAtIndexPathCompletion) {
        return _cellForRowAtIndexPathCompletion(tableView, indexPath);
    }
    else {
        static NSString *CellIdentifier = @"SearchResultListCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        return cell;
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_canEditRowAthIndexPath) {
        return _canEditRowAthIndexPath(tableView, indexPath);
    }
    else {
        return NO;
    }
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_heightForRowAtIndexPathCompletion) {
        return _heightForRowAtIndexPathCompletion(tableView, indexPath);
    }
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_didSelectRowAtIndexPathCompletion) {
        return _didSelectRowAtIndexPathCompletion(tableView, indexPath);
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_didDeselectRowAtIndexPathCompletion) {
        return _didDeselectRowAtIndexPathCompletion(tableView, indexPath);
    }
}

@end
